﻿Imports System.Globalization
Imports System.Threading

<ValidationProperty("Text")> _
Partial Class WebForm_CommonControl_SelectCalendarTextBox
    Inherits MyCalendarTextBoxBase

    Protected Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataBinding
        Me.LoadData()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Me.LoadData()
        End If

    End Sub

    Private Sub LoadData()
        If Me.ddlYear.Items.Count = 0 Then
            Me.BindDDLYear()
            Me.ddlMonth.Items.Insert(0, "--請選擇--")

            Me.ddlDay.Items.Insert(0, "--請選擇--")
        End If

    End Sub

    Private Sub BindDDLYear()

        Dim d As Date = Date.Now.AddYears(Me.MaxYearSteps)

        'For i As Integer = -1 To Me.YearSteps
        For i As Integer = d.Year To Me.MinYear Step -1
            Dim li As New ListItem

            li.Text = d.ToString("yyyy")
            li.Value = d.Year

            Me.ddlYear.Items.Add(li)

            d = d.AddYears(-1)
        Next
        Me.ddlYear.Items.Insert(0, "--請選擇--")
    End Sub

    Private Sub BindDDLMonth()
        Me.ddlMonth.Items.Clear()
        For i As Integer = 1 To 12
            Dim li As New ListItem

            li.Text = i.ToString("00")
            li.Value = i

            Me.ddlMonth.Items.Add(li)
        Next

        Me.ddlMonth.Items.Insert(0, "--請選擇--")

    End Sub

    Private Sub BindDDLDay()
        'Dim c As Calendar = Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar
        Dim c As TaiwanCalendar = Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar
        Me.ddlDay.Items.Clear()

        '是否為閏年2月需分開判斷
        If LeapYear(Int32.Parse(Me.ddlYear.SelectedValue)) = True AndAlso Me.ddlMonth.SelectedValue = "2" Then
            Me.GetDay(29)
        ElseIf LeapYear(Int32.Parse(Me.ddlYear.SelectedValue)) = False AndAlso Me.ddlMonth.SelectedValue = "2" Then
            Me.GetDay(28)
        Else
            Me.GetDay(c.GetDaysInMonth(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue))

        End If


        Me.ddlDay.Items.Insert(0, "--請選擇--")
    End Sub

    Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged
        Me.BindDDLDay()
    End Sub

    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
        Me.BindDDLMonth()
    End Sub
    '某月有某天
    Protected Sub GetDay(ByVal DayNumber As Integer)
        For i As Integer = 1 To DayNumber
            Dim li As New ListItem

            li.Text = i.ToString("00")
            li.Value = i


            Me.ddlDay.Items.Add(li)


        Next
    End Sub
    '判斷是否為閏年
    Protected Function LeapYear(ByVal year As Integer) As Boolean
        If year Mod 400 = 0 Then
            Return True
        ElseIf year Mod 4 = 0 AndAlso year Mod 100 <> 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Property Text() As String
        Get
            Return Me.Value.ToString("yyyy/MM/dd")
        End Get
        Set(ByVal value As String)
            Try
                Me.Value = DateTime.Parse(value)
            Catch ex As Exception

            End Try

        End Set
    End Property

    Public Overrides Property Value() As Date
        Get
            If Me.ddlYear.SelectedIndex = 0 OrElse Me.ddlMonth.SelectedIndex = 0 OrElse Me.ddlDay.SelectedIndex = 0 Then
                Return (New Date(1912, 1, 1))
            Else
                Return (New Date(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue, Me.ddlDay.SelectedValue))
            End If
        End Get
        Set(ByVal value As Date)
            Me.ddlYear.SelectedValue = value.Year
            Me.ddlMonth.SelectedValue = value.Month

            Me.ddlMonth_SelectedIndexChanged(Nothing, Nothing)
            Me.ddlDay.SelectedValue = value.Day
        End Set
    End Property

    'Public Property YearSteps() As Integer
    '    Get
    '        If ViewState("YearSteps") Is Nothing Then
    '            Return 10
    '        End If
    '        Return ViewState("YearSteps")
    '    End Get
    '    Set(ByVal value As Integer)
    '        ViewState("YearSteps") = value
    '    End Set
    'End Property
    Public Property MaxYearSteps() As Integer
        Get
            If ViewState("MaxYearSteps") Is Nothing Then
                Return 1
            End If
            Return ViewState("MaxYearSteps")
        End Get
        Set(ByVal value As Integer)
            ViewState("MaxYearSteps") = value
        End Set
    End Property

    Public Property MinYear() As Integer
        Get
            If ViewState("MinYear") Is Nothing Then
                Return 2000
            End If
            Return ViewState("MinYear")
        End Get
        Set(ByVal value As Integer)
            ViewState("MinYear") = value
        End Set
    End Property
    Public Property Enabled() As Boolean
        Get
            Return False
        End Get
        Set(ByVal value As Boolean)

            Me.ddlYear.Enabled = value
            Me.ddlMonth.Enabled = value

            Me.ddlDay.Enabled = value
        End Set
    End Property
    'public property 
End Class
